package com.jic.manager.controller.manage.merchant;

import com.jic.common.base.vo.Page;
import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.RestResult;
import com.jic.manager.common.util.BeanUtil;
import com.jic.manager.entity.MerchantWeBank;
import com.jic.manager.request.MerchantWeBankRequest;
import com.jic.manager.service.MerchantWeBankService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 商户控制器
 * @author mj
 */
@RestController
@RequestMapping("merchantWeBank")
@Slf4j
public class MerchantWeBankController {

    @Resource
    private MerchantWeBankService merchantWeBankService;

    /**
     * 查询商户列表
     * @param request
     * @return
     */
    @PostMapping("/queryMerchantList")
    @ResponseBody
    @ApiOperation(value = "查询商户列表")
    public RestResult<PageResult<MerchantWeBank>> queryList(@RequestBody MerchantWeBankRequest request) {
        try {

            PageResult<MerchantWeBank> merchantPageResult = merchantWeBankService.queryPage(request, new Page() {{
                setStart(request.getStart());
                setLimit(request.getLimit());
            }});
            return RestResult.success(merchantPageResult);
        }catch (Exception e){
            log.error("Manager queryMerchantList error：",e);
            return RestResult.error("9999","系统异常，列表查询失败");
        }
    }


    /**
     * 商户详情
     * @param request
     * @return
     */
    @PostMapping("/getMerchantInfo")
    @ResponseBody
    @ApiOperation(value = "商户详情")
    public RestResult<MerchantWeBank> getMerchantInfo(@RequestBody MerchantWeBankRequest request) {
        try {
            log.debug("getMerchantInfo " + request);
            MerchantWeBank merchantWeBank = merchantWeBankService.queryByPrimaryKey(request.getId());
            return RestResult.success(merchantWeBank);
        }catch (Exception e){
            log.error("Manager getMerchantInfo error：",e);
            return RestResult.error("9999","系统异常，商户详情查询失败");
        }
    }

    /**
     * 新增商户
     * @param merchantWeBankRequest
     * @return
     */
    @PostMapping(value = "/addMerchantInfo")
    public RestResult<Long> editMerchantInfo(@RequestBody MerchantWeBankRequest merchantWeBankRequest){
        try {
            log.debug("getMerchantInfo " + merchantWeBankRequest);
            return RestResult.success(merchantWeBankService.addMerchantInfo(merchantWeBankRequest));
        }catch (Exception e){
            log.error("Manager addMerchantInfo error：",e);
            return RestResult.error("9999","系统异常，新增商户失败");
        }
    }

    /**
     * 编辑商户信息
     * @param merchantWeBankRequest
     * @return
     */
    @PostMapping("/updateMerchant")
    @ResponseBody
    @ApiOperation(value = "编辑商户信息")
    public RestResult<Boolean> updateMerchant(@RequestBody MerchantWeBankRequest merchantWeBankRequest) {
        try {
            log.debug("updateMerchant " + merchantWeBankRequest);
            return RestResult.success(merchantWeBankService.editMerchantInfo(merchantWeBankRequest));
        }catch (Exception e){
            log.error("Manager updateMerchant error：",e);
            return RestResult.error("9999","系统异常，操作失败");
        }
    }


    /**
     * 商户提交审核
     * @param request
     * @return
     */
    @PostMapping("submitReview")
    public RestResult<Boolean> submitReview(@RequestBody MerchantWeBankRequest request){
        try{
            Boolean flag = merchantWeBankService.submitReview(request);
            return RestResult.success(flag);
        }catch (Exception e){
            log.error("SysRole submitReview error：",e);
            return RestResult.error("9999","系统异常，商户提交审核失败");
        }
    }

    /**
     * 批量提交审核
     * @param request
     * @return
     */
    @PostMapping("batchSubmitReview")
    public RestResult<Boolean> batchSubmitReview(@RequestBody MerchantWeBankRequest request){
        try{
            Boolean flag = merchantWeBankService.batchSubmitReview(request);
            return RestResult.success(flag);
        }catch (Exception e){
            log.error("SysRole batchSubmitReview error：",e);
            return RestResult.error("9999","系统异常，批量提交审核失败");
        }
    }

    /**
     * 撤销提交
     * @param request
     * @return
     */
    @PostMapping("revokeReview")
    public RestResult<Boolean> revokeReview(@RequestBody MerchantWeBankRequest request){
        try{
            Boolean flag = merchantWeBankService.revokeReview(request);
            return RestResult.success(flag);
        }catch (Exception e){
            log.error("SysRole revokeReview error：",e);
            return RestResult.error("9999","系统异常，撤销提交失败");
        }
    }

    /**
     * 审核商户
     * @param request
     * @return
     */
    @PostMapping("merchantReview")
    public RestResult<Boolean> merchantReview(@RequestBody MerchantWeBankRequest request){
        try{
            Boolean flag = merchantWeBankService.merchantReview(request);
            return RestResult.success(flag);
        }catch (Exception e){
            log.error("SysRole merchantReview error：",e);
            return RestResult.error("9999","系统异常，商户审核失败");
        }
    }

    /**
     * 批量审核商户
     * @param request
     * @return
     */
    @PostMapping("batchMerchantReview")
    public RestResult<Boolean> batchMerchantReview(@RequestBody MerchantWeBankRequest request){
        try{
            Boolean flag = merchantWeBankService.batchMerchantReview(request);
            return RestResult.success(flag);
        }catch (Exception e){
            log.error("SysRole batchMerchantReview error：",e);
            return RestResult.error("9999","系统异常，批量审核商户失败");
        }
    }



    /**
     * 删除商户
     * @param request
     * @return
     */
    @PostMapping("deleteMerchantInfo")
    public RestResult<Long> deleteMerchantInfo(@RequestBody MerchantWeBankRequest request){
        try{
            Long flag = merchantWeBankService.delete(request.getId());
            return RestResult.success(flag);
        }catch (Exception e){
            log.error("SysRole deleteMerchantInfo error：",e);
            return RestResult.error("9999","系统异常，删除商户失败");
        }
    }


}
